#include "common.h"
#include "main.h"
#include "functions.h"

INT APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
	//Ignore UNREFERENCED PARAMETER Info
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	//Initialize Common Controls
	InitCommonControls();

	// Store instance handle in our global variable
	hInst = hInstance;

	action_InitBeforeUI();

	hWndMain = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG_CONFIG), NULL, (DLGPROC) WndProc); 

	action_InitAfterUI();

	ShowWindow(hWndMain, SW_SHOW);
	// Main message loop:
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (IsDialogMessage(hWndMain, &msg)) continue;
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}

INT_PTR CALLBACK WndProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message) {
	case WM_INITDIALOG:
		SendMessage( hDlg, (UINT) WM_SETICON, (WPARAM) ICON_SMALL, (LPARAM) hIcon_S);
		SendMessage( hDlg, (UINT) WM_SETTEXT, (WPARAM) NULL, (LPARAM) STRING_UI_SRUNX);
		SendMessage( GetDlgItem(hDlg, IDC_COMBO_MODE), (UINT) CB_ADDSTRING, (WPARAM) NULL, (LPARAM) STRING_UI_MODE_0);
		SendMessage( GetDlgItem(hDlg, IDC_COMBO_MODE), (UINT) CB_ADDSTRING, (WPARAM) NULL, (LPARAM) STRING_UI_MODE_1);
		SendMessage( GetDlgItem(hDlg, IDC_COMBO_MODE), (UINT) CB_ADDSTRING, (WPARAM) NULL, (LPARAM) STRING_UI_MODE_2);
		action_ModifyUIbyConfigInfo(&config_info, hDlg);
		action_ModifyUIbyState(client->getState(), hDlg);
		return (INT_PTR)TRUE;
	case WM_NOTIFYICON:
		switch (LOWORD(lParam)) {
		case WM_LBUTTONUP:
			ShowWindow(hDlg, SW_SHOW);
			ShowWindow(hDlg, SW_RESTORE);
			SetForegroundWindow(hDlg);
			break;
		case WM_RBUTTONUP: 
			{
				POINT point;
				GetCursorPos(&point);
				SetForegroundWindow(hDlg);
				TrackPopupMenu(GetSubMenu(hMenuTray, 0), TPM_RIGHTALIGN, point.x, point.y, 0, hDlg, NULL);
				PostMessage(hDlg, WM_NULL, 0, 0); 
			} break;
		}
		break;
	case WM_COMMAND: 
		{
			WORD wmId    = LOWORD(wParam);
			WORD wmEvent = HIWORD(wParam);
			switch(wmId) {
				case IDC_BUTTON_LOGIN:
					if (client->getState() == 0) {
						funvtion_ConvertConfigInfoToClientInfo(&config_info, &client_info);
						client->login(&client_info);
					} else {
						client->logout();
					}
					action_ModifyUIbyState(client->getState(), hDlg);
					break;
				case IDC_BUTTON_SAVE:
					action_SaveConfig();
					break;
				case IDC_CHECK_SAVEUN:
					if (!IsDlgButtonChecked(hDlg, IDC_CHECK_SAVEUN)) {
						CheckDlgButton(hDlg, IDC_CHECK_LOGAUT, BST_UNCHECKED);
						CheckDlgButton(hDlg, IDC_CHECK_SAVEPW, BST_UNCHECKED);
					}
					action_ModifyConfigInfobyUI(&config_info, hDlg);
					break;
				case IDC_CHECK_SAVEPW:
					if (IsDlgButtonChecked(hDlg, IDC_CHECK_SAVEPW)) CheckDlgButton(hDlg, IDC_CHECK_SAVEUN, BST_CHECKED);
					else CheckDlgButton(hDlg, IDC_CHECK_LOGAUT, BST_UNCHECKED);
					action_ModifyConfigInfobyUI(&config_info, hDlg);
					break;
				case IDC_CHECK_LOGAUT:
					if (IsDlgButtonChecked(hDlg, IDC_CHECK_LOGAUT)) {
						CheckDlgButton(hDlg, IDC_CHECK_SAVEUN, BST_CHECKED);
						CheckDlgButton(hDlg, IDC_CHECK_SAVEPW, BST_CHECKED);
					}
				case IDC_CHECK_FOURCE:
				case IDC_CHECK_RELINK:
				case IDC_COMBO_MODE:
					action_ModifyConfigInfobyUI(&config_info, hDlg);
					break;
				case IDC_BUTTON_EXIT:
					exit(0);
				default: break;
			}
			// Parse the menu selections:
		} break;
	case WM_SIZE:
		if (wParam == SIZE_MINIMIZED) ShowWindow(hDlg, SW_HIDE);
		break;
	case WM_CLOSE:
		Shell_NotifyIcon(NIM_DELETE, &NotifyTray);
		action_SaveConfig();
		EndDialog(hDlg, LOWORD(wParam));
		PostQuitMessage(0);
		break;
	}
	return (INT_PTR)FALSE;
}

